package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.GetCurrentDay;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.common.allenum.OrderStatus;
import com.example.restaurant.dao.OrderInfoMapper;
import com.example.restaurant.model.OrderInfo;
import com.example.restaurant.model.Orders;
import com.example.restaurant.dao.OrderMapper;
import com.example.restaurant.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.dto.OrderInfoDto;
import com.example.restaurant.service.param.AddOrderParam;
import com.example.restaurant.service.param.ModifyOrderParam;
import com.example.restaurant.service.param.QueryOrderParam;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrdersService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Override
    @Transactional
    public String addOrder(AddOrderParam addOrderParam) {

        Orders orders = BeanUtil.copyProperties(addOrderParam, Orders.class);


        orders.setOrderStatus(OrderStatus.NEW_ORDER.toString());
        SimpleDateFormat dmf = new SimpleDateFormat("yyyyMMdd");
        String fetch=null;
        //截取当前时间作为流水号
        Date date = new Date();
        String preCode = dmf.format(date);
        String maxCode = orderMapper.getMaxCode(GetCurrentDay.getCurrentDay());
        //设置流水号格式，根据实际情况设置
        DecimalFormat num=new DecimalFormat("000");
        if(maxCode != null && !"null".equals(maxCode)){
            //对流水号截取后三位
            String code = maxCode.substring(maxCode.length()-3,maxCode.length());
            //例如后三位为002，通过以下步骤，将会变为003
            int count = Integer.valueOf(code)+1;
            String number = num.format(count);
            fetch= preCode + "-" + number;
        }else {
            fetch =  preCode + "-" + "001";
        }
        String s = IdUtil.randomUUID();
        orders.setFetchNumber(fetch);
        orders.setOrderNumber(s);
        orders.setOrderTime(date);
        orders.setOrderStatus(OrderStatus.NEW_ORDER.toString());
        orderMapper.addOrder(orders);
        OrderInfo orderInfo = new OrderInfo();
        for (OrderInfoDto info :addOrderParam.getOrderForm()) {
            orderInfo.setOrderNumber(s);
            orderInfo.setDishId(info.getId());
            orderInfo.setDishNumber(info.getDishNumber());
            orderInfo.setDishPrice(info.getDishPrice());
            orderInfo.setDishImg(info.getDishImg());
            orderInfo.setDishName(info.getDishName());
            orderInfoMapper.insert(orderInfo);

        }
        return s;
    }

    @Override

    public Page<Orders> queryOrderByOrderNumber(String orderNumber,Integer current,Integer size,Integer storeId) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.like("order_number",orderNumber);
        wrapper.eq("store_id",storeId);
        Page<Orders> orderPage = orderMapper.selectPage(new Page<>(current, size), wrapper);
        if (orderPage.getRecords().size()==0){
            throw new RestaurantException("没有该订单号");
        }
        return orderPage;
    }

    @Override
    @Transactional
    public void modifyOrder(ModifyOrderParam param) {
        int i = orderMapper.modifyOrder(param);
        if (i<1){
            throw new RestaurantException("修改订单状态失败");
        }
    }

    @Override
    public Orders getOrder(String orderNumber) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        return orderMapper.selectOne(wrapper);
    }
}
